package com.example.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.bean.Dish;
import com.example.reggie.bean.DishFlavor;
import com.example.reggie.bean.Setmeal;
import com.example.reggie.bean.SetmealDish;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.service.DishFlavorService;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import com.example.reggie.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(name==null?false:true,Setmeal::getName,name);
        setmealService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }

    @PostMapping
    @Transactional
    public R<String> add(@RequestBody SetmealDto setmealDto){
        //保存菜单表
        Setmeal setmeal=(Setmeal) setmealDto;
        setmealService.save(setmeal);

        //保存套餐菜品表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(item->{
            item.setSetmealId(setmeal.getId());
        });
        setmealDishService.saveBatch(setmealDishes);

        return R.success("添加套餐成功");
    }

    @GetMapping("/{id}")
    @Transactional
    public R<SetmealDto> getOne(@PathVariable Long id){
        //套餐菜品表数据
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes=setmealDishService.list(setmealDishLambdaQueryWrapper);
        //查询套餐数据
        Setmeal setmeal=setmealService.getById(id);
        //数据合并
        SetmealDto setmealDto =new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

    @PutMapping
    @Transactional
    public R<String> update(@RequestBody SetmealDto setmealDto){
        //清空套餐菜品表相关数据
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        //重新添加套菜产品表
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.forEach(item->{
            item.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishList);
        //更新套餐表
        setmealService.updateById(setmealDto);
        return R.success("更新套餐成功");
    }
    @PostMapping("/status/{type}")
    public R<String> status(@PathVariable int type,Long[] ids){
        List<Long> idList=new ArrayList<>();
        for (Long id:ids) {
            idList.add(id);
        }
        List<Setmeal> setmealList=setmealService.listByIds(idList);
        setmealList.forEach(item->{
            item.setStatus(type);
        });

        setmealService.updateBatchById(setmealList);
        return R.success("更新套餐状态成功");
    }

    @DeleteMapping
    public R<String> delete(Long[] ids){
        List<Long> idList= new ArrayList<>();

        //删除套餐菜品
        for (Long id:ids) {
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
            setmealDishService.remove(setmealDishLambdaQueryWrapper);
            idList.add(id);
        }
        //删除套餐
        setmealService.removeByIds(idList);
        return R.success("删除套餐成功");
    }

    @GetMapping("/list")
    public R<List> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> setmealList=setmealService.list(queryWrapper);
        List<SetmealDto> list=setmealList.stream().map(item->{
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            LambdaQueryWrapper<SetmealDish> queryWrapper_=new LambdaQueryWrapper<>();
            queryWrapper_.eq(SetmealDish::getSetmealId,item.getId());
            List<SetmealDish> setmealDishList=setmealDishService.list(queryWrapper_);
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }).collect(Collectors.toList());
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R<List> dishList(@PathVariable("id") String id){
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list=setmealDishService.list(queryWrapper);

        return R.success(list);
    }
}
